//引入VueRouter
import VueRouter from 'vue-router'
import storage from '@/utils/storage'
import Bmob from 'hydrogen-js-sdk'
import secret from '@/utils/secret'

let routes=[
    {
        name:'login',
        path:'/login',
        component:()=>import('../views/loginPager'),
        meta:{title:'登录'}
    },
    {
        name:'home',
        path:'/',
        redirect: 'dataStatisticsView',
        component:()=>import('../views/homePager'),
        meta:{title:'主页'},
        children:[
            {
                name:'stockInManageView',
                path:'stockInManageView',
                meta:{title:'入库管理'},
                component:()=>import('../views/routerview/stockInManageView')
            },
            {
                name:'stockOutManageView',
                path:'stockOutManageView',
                meta:{title:'出库管理'},
                component:()=>import('../views/routerview/stockOutManageView')
            },
            {
                name:'dataStatisticsView',
                path:'dataStatisticsView',
                meta:{title:'数据看板'},
                component:()=>import('../views/routerview/dataStatisticsView')
            },
            {
                name:'partsManageView',
                path:'partsManageView',
                meta:{title:'配件管理'},
                component:()=>import('../views/routerview/partsManageView')
            },
            {
                name:'farmManageView',
                path:'farmManageView',
                meta:{title:'车间管理'},
                component:()=>import('../views/routerview/farmManageView')
            },
            {
                name:'wareHouseManageView',
                path:'wareHouseManageView',
                meta:{title:'仓库管理'},
                component:()=>import('../views/routerview/wareHouseManageView')
            },
            {
                name:'personManageView',
                path:'personManageView',
                meta:{title:'人员管理'},
                component:()=>import('../views/routerview/personManageView')
            },
            {
                name:'roleManageView',
                path:'roleManageView',
                meta:{title:'角色管理'},
                component:()=>import('../views/routerview/roleManageView')
            }             
        ]
    }

]


const router = new VueRouter({
    routes
})

//全局前置路由守卫
router.beforeEach((to, from, next) => {
    let user = Bmob.User.current()
    // let xs = storage.getLocal('xx')
    //保证本地数据不被修改
    if(user){
        if(to.path=='/login'){
            next('/')
        }else{
            console.log(to);
            next()
        }
    }else{
        if(to.path=='/login'){
            next()
        }else{
            next('/login')
        }
    }
  })

//全局后置路由守卫
router.afterEach((to,from)=>{
	document.title = to.meta.title || '手表配件库存后台管理系统'
})

export default router
